package ciyuan.Gui.Particle;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.network.PacketBuffer;
import net.minecraft.particles.IParticleData;
import net.minecraft.particles.ParticleType;
import net.minecraft.util.math.vector.Vector3d;

import java.util.Locale;

public class DamageParticleData implements IParticleData {

    public enum DamageType {
        physicsNormal,
        physicsCrital,
        magicNormal,
        magicCrital,
    }

    private final Vector3d speed;
    private final int count;
    private final DamageType damageType;
    public static final IDeserializer<DamageParticleData> DESERIALIZER = new IDeserializer<DamageParticleData>() {

        @Override
        public DamageParticleData fromCommand(ParticleType<DamageParticleData> p_197544_1_, StringReader reader) throws CommandSyntaxException {
            final int MIN_COLOUR = 0;
            final int MAX_COLOUR = 255;
            reader.expect(' ');
            double speedX = reader.readDouble();
            reader.expect(' ');
            double speedY = reader.readDouble();
            reader.expect(' ');
            double speedZ = reader.readDouble();
            reader.expect(' ');
            int count = reader.readInt();
            return new DamageParticleData(new Vector3d(speedX, speedY, speedZ),count,DamageType.physicsNormal);
        }

        @Override
        public DamageParticleData fromNetwork(ParticleType<DamageParticleData> p_197543_1_, PacketBuffer buffer) {
            final int MIN_COLOUR = 0;
            final int MAX_COLOUR = 255;
            double speedX = buffer.readDouble();
            double speedY = buffer.readDouble();
            double speedZ = buffer.readDouble();
            int count = buffer.readInt();
            int typeInt = buffer.readInt();
            DamageType type = DamageType.physicsNormal;
            if (typeInt==1) {
                type = DamageType.physicsNormal;
            } else if (typeInt==2) {
                type = DamageType.physicsCrital;
            } else if (typeInt==3) {
                type = DamageType.magicNormal;
            } else if (typeInt==4) {
                type = DamageType.magicCrital;
            }
            return new DamageParticleData(new Vector3d(speedX, speedY, speedZ),count,type);
        }


    };

    public DamageParticleData(Vector3d speed,int count,DamageType type) {
        this.speed = speed;
        this.count = count;
        this.damageType = type;
    }

    public DamageType getDamageType() {
        return damageType;
    }

    @Override
    public ParticleType<?> getType() {
        return ParticleRegistry.damageParticle.get();
    }

    @Override
    public void writeToNetwork(PacketBuffer buffer) {
        buffer.writeDouble(this.speed.x);
        buffer.writeDouble(this.speed.y);
        buffer.writeDouble(this.speed.z);
        buffer.writeInt(this.count);

        if (this.damageType==DamageType.physicsNormal) {
            buffer.writeInt(1);
        } else if (this.damageType==DamageType.physicsCrital) {
            buffer.writeInt(2);
        } else if (this.damageType==DamageType.magicNormal) {
            buffer.writeInt(3);
        } else if (this.damageType==DamageType.magicCrital) {
            buffer.writeInt(4);
        }
    }

    @Override
    public String writeToString() {
        return String.format(Locale.ROOT, "%s %.2d %.2d %.2d %d",
                this.getType().getRegistryName(),  speed.x, speed.y, speed.z,count);
    }

    public Vector3d getSpeed() {
        return speed;
    }

    public int getCount() {
        return count;
    }
}
